package 队列.练习;
//循环队列
//浪费一个空间，来判断
public class MyCircularQueue {
    private int[] elem;
    //标记删除的下标
    private int front;
    //标记添加的下标
    private int rear;

    public MyCircularQueue(int k ) {
        //要多给一个，因为会浪费一个空间
        this.elem = new int[k+1];
    }

    public boolean isEmpty(){
        if(rear == front ){
            return true;
        }
        return false;
    }
    public boolean isFull(){
        if((rear+1)% elem.length ==front){
            return true;
        }
        return false;
    }
    public boolean enQueue(int val){
        if(isFull()){
            return false;
        }else {
            elem[rear] = val;
            rear = (rear+1)%elem.length;
        }
        return true;
    }
    public boolean deQueue(){
        if(isEmpty()){
            return false;
        }
        elem[front]= 0;
        front = (front+1)%elem.length;
        return true;
    }
    public int Front(){
        if(isEmpty()){
            return -1;
        }
        return elem[front];
    }
    public int Rear(){
        if(isEmpty()){
            return -1;
        }
        int index = 0;
        if(rear ==0){
            index = elem.length-1;
        }else {
            index = rear-1;
        }
        return elem[index];
    }

}